package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.mystery;

import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.world.ServerWorld;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

public class MirrorSkill extends BaseSkill {

    public MirrorSkill(int level) {
        super(level);
    }

    public MirrorSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public MirrorSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMaxUseCount(1);
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return "镜像技能，生成一个自己的克隆体。";
    }

    @Override
    public SkillRarity getSkillRarity() {
        return SkillRarity.Mystery;
    }

    @Override
    public boolean canUseAbility(MobEntity mobEntity) {
        return mobEntity.getTarget() != null && getSkillUsecount() < getMaxUseCount();
    }

    @Override
    public boolean onAbilityUse(MobEntity mobEntity) {
        if (canUseAbility(mobEntity)) {
            setSkillUsecount(getSkillUsecount() + 1);
            NbtCompound nbtCompound = new NbtCompound();
            NbtCompound compound = new NbtCompound();
            compound = mobEntity.writeNbt(compound);
            if (compound.contains("UUID")) compound.remove("UUID");
            nbtCompound.put("EntityTag", compound);
            mobEntity.getType().spawn((ServerWorld) mobEntity.getEntityWorld(), nbtCompound, mobEntity.getName(), null, mobEntity.getBlockPos(), SpawnReason.REINFORCEMENT, true, true);
        }
        return true;
    }
}
